package options

import (
	"fmt"
	"photowall/pkg/config"
	"photowall/pkg/log"
	"sync"

	"github.com/go-redis/redis"
)

var (
	redisClientSyncOnce sync.Once
	redisClient         *redis.Client
)

// 单例模式实现的获取 redisClient
func GetRedisClient() *redis.Client {
	redisClientSyncOnce.Do(func() {
		DimlightGlobalConfig := config.GetConfig()

		redisClient = redis.NewClient(&redis.Options{
			Addr:     DimlightGlobalConfig.ServiceConfig.RedisConfig.RedisAddr,     // redis地址
			Password: DimlightGlobalConfig.ServiceConfig.RedisConfig.RedisPassword, // 密码
			DB:       DimlightGlobalConfig.ServiceConfig.RedisConfig.RedisDB,       // 使用默认数据库
		})

		res, _ := redisClient.ConfigGet("appendfsync").Result()

		fmt.Print(res)

		err := redisClient.ConfigSet("save",
			DimlightGlobalConfig.ServiceConfig.RedisConfig.SaveOpt).Err()
		if err != nil {
			log.GetRedisLogger().Error(err.Error() + " " +
				DimlightGlobalConfig.ServiceConfig.RedisConfig.SaveOpt)
			panic(err)
		}

		err = redisClient.ConfigSet("appendfsync",
			DimlightGlobalConfig.ServiceConfig.RedisConfig.SaveWay).Err()
		if err != nil {
			log.GetRedisLogger().Error(err.Error() + " " +
				DimlightGlobalConfig.ServiceConfig.RedisConfig.SaveWay)
			panic(err)
		}
	})

	return redisClient
}
